<?php
  /***************************************************************
   * @file: main.php
   * @author: Neo
   * @mail: xq262144@gmail.com
   * @date: 
   * @description:
   * this file is the error controlling model
   ***************************************************************/
      
  if (!defined('SYS_BASEPATH')) {echo "No script access allowed!"; header("HTTP/1.0 404 Not Found"); }    
  
  function generate_pagination($url_base, $element_count, $per_page, $current_page){
      $element_count = intval($element_count);
      $per_page = intval($per_page);
      $current_page = intval($current_page);
      $pagination_suffix = "";
      $pagination_prefix = "<div>";
      $pagination_block = "";
      $result = array('block' => '', 'db_suffix' => null);
      if (0 == $per_page) $per_page = 20;
      $page_count = ceil($element_count / $per_page);
      $page_previous_url =
<<<BLOCK
<a href="%s">上一页</a>
BLOCK;
      $page_next_url = 
<<<BLOCK
<a href="%s">下一页</a>
BLOCK;
      $page_base_url = 
<<<BLOCK
<a href="%s">%d</a>
BLOCK;
      $page_current_url = 
<<<BLOCK
<span>%d</span>
BLOCK;
      if ($page_count <= 1){
          return $result;
      }
      if ($current_page < 1 || $current_page > $page_count) {
          $current_page = 1;
      }
      //check page num. to determine pagination url style
      if ($current_page != 1){
          $temp_count = $page_count - 1;
          $pagination_prefix .= sprintf($page_previous_url, $url_base . "/$temp_count");
      }
      
      if ($current_page != $page_count){
          $temp_count = $current_page + 1;
          $pagination_suffix = sprintf($page_next_url, $url_base . "/$temp_count");
      }
      
      $pagination_suffix .= '</div>';
      
      for($i = 1; $i != $page_count + 1; ++$i){
          if ($i != $current_page)
              $pagination_block .= sprintf($page_base_url, $url_base . "/$i", $i);
          else
              $pagination_block .= sprintf($page_current_url, $i);
      }
      
      $pagination_display = $pagination_prefix . $pagination_block . $pagination_suffix;
      $result = array();
      $result['block'] = $pagination_display;
      $result['db_suffix'] = array(($current_page - 1) * $per_page, $per_page);
      return $result;
  }
  
?>